package com.example.dllo.baidumusic.main;
/*
                   _ooOoo_
                  o8888888o
                  88" . "88
                  (| -_- |)
                  O\  =  /O
               ____/`---'\____
             .'  \\|     |//  `.
            /  \\|||  :  |||//  \
           /  _||||| -:- |||||-  \
           |   | \\\  -  /// |   |
           | \_|  ''\---/''  |   |
           \  .-\__  `-`  ___/-. /
         ___`. .'  /--.--\  `. . __
      ."" '<  `.___\_<|>_/___.'  >'"".
     | | :  `- \`.;`\ _ /`;.`/ - ` : | |
     \  \ `-.   \_ __\ /__ _/   .-` /  /
======`-.____`-.___\_____/___.-`____.-'======
                   `=---='
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
         佛祖保佑       永无BUG
*/

import android.app.Activity;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore;
import android.support.v7.app.AlertDialog;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.example.dllo.baidumusic.R;
import com.example.dllo.baidumusic.nettool.BaseActivity;
import com.example.dllo.baidumusic.nettool.MyUser;

import java.io.File;

import cn.bmob.v3.BmobUser;
import cn.bmob.v3.exception.BmobException;
import cn.bmob.v3.listener.UpdateListener;

/**
 * Created by dllo on 16/9/2.
 */
public class UserLogihActivity extends BaseActivity {
    private TextView out;
    private TextView loginKnow;
    private BmobUser bmobUser;
    private Bitmap myPhoto;
    private ImageView photo;
    private MyUser myUser;


    @Override
    protected int setLayout() {
        return R.layout.activity_user;

    }

    @Override
    protected void initView() {
        out = bindView(R.id.out_login);
        photo = bindView(R.id.me_login_photo);
        photo.setOnClickListener(this);
        out.setOnClickListener(this);
        myUser = BmobUser.getCurrentUser(MyUser.class);
        bmobUser = BmobUser.getCurrentUser();
        loginKnow = bindView(R.id.login_know);
        if (bmobUser != null) {
            loginKnow.setText(bmobUser.getUsername());
            if (myUser.getIcon() == null) {
                photo.setImageResource(R.mipmap.ic_baidu);
            } else {
                photo.setImageBitmap(myUser.getIcon());
            }

        } else {
            loginKnow.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent = new Intent(UserLogihActivity.this, MeLoghActivity.class);
                    startActivity(intent);
                    finish();
                }
            });

        }
    }

    @Override
    protected void onResume() {
        super.onResume();

    }


    @Override
    protected void onRestart() {
        super.onRestart();
        bmobUser = BmobUser.getCurrentUser();
        loginKnow.setText(bmobUser.getUsername());
        loginKnow.setClickable(false);
    }

    @Override
    protected void initData() {

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.out_login:
                showDiglog();
                break;
            case R.id.me_login_photo:
                showDiglogPhoto();

        }
    }

    private void showDiglogPhoto() {
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        final AlertDialog dialog = builder.create();
        View view = View.inflate(getApplicationContext(), R.layout.dialog_show, null);
        TextView cream = (TextView) view.findViewById(R.id.cream);
        TextView bendixiangce = (TextView) view.findViewById(R.id.bendi);
        // 在相册中选取
        bendixiangce.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent1 = new Intent(Intent.ACTION_PICK, null);
                intent1.setDataAndType(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, "image/*");

                startActivityForResult(intent1, 1);
                dialog.dismiss();
            }
        });
        // 调用照相机
        cream.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent2 = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
                intent2.putExtra(MediaStore.EXTRA_OUTPUT,
                        Uri.fromFile(new File(Environment.getExternalStorageDirectory(), "head.jpg")));
                startActivityForResult(intent2, 2);// 采用ForResult打开
                dialog.dismiss();
            }
        });
        dialog.setView(view);
        dialog.show();
    }


    private void showDiglog() {
        new AlertDialog.Builder(this).setTitle("是否退出当前账号?")
                .setIcon(android.R.drawable.ic_dialog_info)
                .setPositiveButton("是", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        Intent newIntent = new Intent(getApplicationContext(), UserLogihActivity.class);
                        startActivity(newIntent);
                        BmobUser.logOut();
                        BmobUser current = BmobUser.getCurrentUser();

                    }
                })
                .setNegativeButton("否", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        Toast.makeText(UserLogihActivity.this, "你没有退出", Toast.LENGTH_SHORT).show();
                    }
                }).show();

    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {

        switch (requestCode) {
            case 1:
                cropPhoto(data.getData());
                break;
            case 2:
                File temp = new File(Environment.getExternalStorageDirectory() + "/head.jpg");
                cropPhoto(Uri.fromFile(temp));
                break;
            case 3:
                Bundle extras = data.getExtras();
                myPhoto = extras.getParcelable("data");
                photo.setImageBitmap(myPhoto);
                MyUser myUsers = new BmobUser().getCurrentUser(MyUser.class);
                if (myUsers != null) {
                    myUsers.setIcon(myPhoto);
                    Log.d("UserLogihActivity", "vampire");
                    myUsers.update(new UpdateListener() {
                        @Override
                        public void done(BmobException e) {
                            if (e == null) {
                                Toast.makeText(UserLogihActivity.this, "头像上传成功", Toast.LENGTH_SHORT).show();
                                photo.setImageBitmap(myPhoto);
                            } else {
                                Toast.makeText(UserLogihActivity.this, "头像上传失败", Toast.LENGTH_SHORT).show();
                            }
                        }
                    });
                }
                break;


        }
        super.onActivityResult(requestCode, resultCode, data);
    }

    public void cropPhoto(Uri uri) {
        Intent intent = new Intent("com.android.camera.action.CROP");
        intent.setDataAndType(uri, "image/*");
        intent.putExtra("crop", "true");
        // aspectX aspectY 是宽高的比例
        intent.putExtra("aspectX", 1);
        intent.putExtra("aspectY", 1);
        // outputX outputY 是裁剪图片宽高
        intent.putExtra("outputX", 150);
        intent.putExtra("outputY", 150);
        intent.putExtra("return-data", true);
        startActivityForResult(intent, 3);
    }

}
